﻿using APILibrary.TypeDic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TsInterfaceRazor.analysis;

namespace RazorTs.analysis
{
  
    /// <summary>
    /// 解析类型
    /// </summary>
    public static class AnalysisType
    {
        public static List<TsEnum> tsEnums = new List<TsEnum>();
        //处理通用泛型类型
        public static Dictionary<string, string> GenericsType = new Dictionary<string, string>
        {
            ["PageList"] = "PageList<{0}>",
            ["ModelEntity"] = "ModelEntity<{0}>",
            ["ListEntity"] = "ListEntity<{0}>",
        };
        public static TypeInfo Start(Type typeInfo)
        {
            if (typeInfo.IsPrimitive || typeInfo.Name=="String")//是否是基元类型
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.BaseType };
            }
            if (typeInfo.IsClass && !typeInfo.IsPrimitive && !typeInfo.IsGenericType)
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.Class };
            }
            if (typeInfo.IsArray)
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.IsArray };
            }
            if (typeInfo.IsEnum)
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.Enum };
            }
            if (typeInfo.IsIEnumerableType())
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.IEnumerable };
            }
            if (typeInfo.IsGenericType && !typeInfo.IsNullableType())
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.Generic };
            }
            else
            {
                return new TypeInfo { Type = typeInfo, TypeEnum = TypeEnum.Other };
            }
        }

        public static bool IsT { get; set; } = false;

        public static string GetMethodReturn(Type type)
        {
            var stringName = "";
            //获取类型
            var info=Start(type);
            //分析类型
            switch (info.TypeEnum)
            {
                case TypeEnum.Class:
                    stringName = info.Type.Name;
                    break;
                case TypeEnum.BaseType:
                    stringName =TypeDic.TsTypeDic[info.Type.Name];
                    break;
                case TypeEnum.IEnumerable: 
                case TypeEnum.IsArray:
                    stringName = "Arry<{0}>";
                    var result = GetMethodReturn(type.GetGenericArguments()[0]);
                    stringName = string.Format(stringName, result);
                    break;
                case TypeEnum.Generic:
                    var sRetrun = info.Type.Name;
                    sRetrun = sRetrun.Replace("`1", "");
                    if (GenericsType.ContainsKey(sRetrun))//包含自定义的参数
                    {
                        IsT = true;
                        stringName = string.Format(GenericsType[sRetrun], GetMethodReturn(type.GetGenericArguments()[0]));
                        return stringName;
                    }
                        stringName = GetMethodReturn(type.GetGenericArguments()[0]);
                        return stringName;
                    //break;
                case TypeEnum.Enum:
                    break;
                case TypeEnum.Other:
                    stringName = "any";
                    break;
                default:
                    break;
            }
            return stringName;
        }

        public static void GenerateEnum(Type type)
        {
            var result = Start(type);
            if (result.TypeEnum == TypeEnum.Enum)
            {
                TsEnum tsEnum = new TsEnum();
                tsEnum.Describe= ToTs.GetDescribe(type);
                tsEnum.Name = type.Name;
                List<TsEnumFiled> TsEnumFiled = new List<TsEnumFiled>();
                foreach (var v in type.GetFields())
                {
                    if (v.FieldType.IsEnum == true)
                    {
                        TsEnumFiled.Add(new analysis.TsEnumFiled 
                        {
                            Describe= ToTs.GetDescribe(v.FieldType),
                            EnumName= v.Name,
                            Value = v.GetRawConstantValue()
                        });
                        Console.WriteLine(v.Name + ":" + v.GetRawConstantValue());
                    }
                }
                tsEnum.TsEnumFiled = TsEnumFiled;
                tsEnums.Add(tsEnum);
            }
           
        }

    }

    public class TsEnumFiled
    {
        /// <summary>
        ///名称
        /// </summary>
        public string EnumName { get; set; }
        /// <summary>
        /// 值
        /// </summary>
        public object Value { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Describe { get; set; }
    }

    public class TsEnum
    {
        /// <summary>
        /// 描述
        /// </summary>
        public string Describe { get; set; }
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public IEnumerable<TsEnumFiled> TsEnumFiled { get; set; } = new List<TsEnumFiled>();

    }





    public class TypeInfo
    {
        public TypeEnum TypeEnum { get; set; }
        public Type Type { get; set; }
    }

    public enum TypeEnum
    {
        //类
        Class=1,
        //基本类型
        BaseType=2,
        //IEnumerable List
        IEnumerable = 3,
        //arry
        IsArray=4,
        //泛型
        Generic =5,
        //enum
        Enum=6,
        //其它类型
        Other=7,
    }
}
